#!/bin/sh

#The problem is that the mtd driver will only accept writes with length 
#equal a multiple of 2048, so the last write operation will always fail 
#(unless you are very lucky with the size). To get past the problem you 
#have to pad the uImage file with some extra 0xff.

#You can write a program that does this nicely or you can just do the 
#following hack:
#Run the following command for a while (stop it with ctrl-c) to generate 
#a file containing only 0xff
#  while true ; do echo -n xx | tr 'x' '\377' >> ff.file; done
#The size should be at least 2048 bytes, but it does not matter if it is 
#too big. 
#You will still get the error message, but it is after the end of the 
#code so it does not matter.

if [ ! $1 ]; then
	echo " You did not provide a kernel! "
    exit 1
fi

if [ ! -f $1 ]
then
    echo " File $1 does not exists! " 
    exit 1
fi

if [ ! -f ff.file ]
then
    for i in `seq 1 1024` ; do echo -n xx | tr 'x' '\377' >> ff.file; done
fi


echo " Very well... I will flash $1 "
#Then pad the kernel

echo " cat $1 ff.file > big_uImage.bin "
cat $1 ff.file > big_uImage.bin

#And now you can download the kernel with
#cat uImagePad.bin > /dev/mtd3

echo " flash_eraseall /dev/mtd3 "
flash_eraseall /dev/mtd3
echo " nandwrite -p /dev/mtd3 big_uImage.bin "
nandwrite -p /dev/mtd3 big_uImage.bin
